import axios from 'axios'
import baseUrl from '@/config/utls'

const defaultOpt = {login: true}

function baseRequest(options) {
    let instance = '';
    if (!options) return false
    switch (options.urlType) {
        case 'api':
            instance = axios.create({
                baseURL: import.meta.env.VITE_APP_URL,
                timeout: 60000,
                withCredentials: true,
                headers: {
                    // 设置后端需要的传参类型
                    'Content-Type': 'application/json',
                    // 'token': 'your token',
                    'X-Requested-With': 'XMLHttpRequest',
                },
            })
            break;
        default:
            instance = axios.create({
                baseURL: import.meta.env.VITE_APP_URL,
                timeout: 60000,
                withCredentials: true,
                headers: {
                    // 设置后端需要的传参类型
                    'Content-Type': 'application/json',
                    // 'token': 'your token',
                    'X-Requested-With': 'XMLHttpRequest',
                },
            })
    }
    axios.defaults.withCredentials = true
    // const token = $store.state.app.token
    // console.log(token)
    const headers = options.headers || {}
    // headers['Authorization'] =  token
    // headers["Authori-zation"] = "Bearer" + token
    options.headers = headers
    // 判断未登录
    // if (options.login && !token) {
    //     // qudengl
    //     toLogin()
    //     return Promise.reject({ msg: '未登录', toLogin: true })
    // }
    // console.log(options)
    return instance(options).then(res => {
        const data = res.data || {}
        if (res.status !== 200) {
            return Promise.reject({msg: "请求失败", res, data})
        }
        if (['410000', '410001', '410002'].indexOf(data.code) !== -1) {
            // 去登录
            // toLogin()
            return Promise.reject({msg: res.data.msg, res, data, toLogin: true})
        } else if (data.code === '200') {
            return Promise.resolve(data, res)
        } else {
            return Promise.reject({msg: res.data.msg, res, data})
        }
    })
}

/**
 * http 请求基础类
 * 参考文档 https://www.kancloud.cn/yunye/axios/234845
 *
 */
const request = ["post", "put", "patch"].reduce((request, method) => {
    /**
     *
     * @param url string 接口地址
     * @param data object get参数
     * @param options object axios 配置项
     * @param urlType object axios 请求接口路径域名区分
     * @returns {AxiosPromise}
     */
    request[method] = (url, data = {}, options = {}, urlType) => {
        return baseRequest(
            Object.assign({url, data, method, urlType}, defaultOpt, options)
        );
    };
    return request;
}, {});

["get", "delete", "head"].forEach(method => {
    /**
     *
     * @param url string 接口地址
     * @param params object get参数
     * @param options object axios 配置项
     * @param urlType object axios 请求接口路径域名区分
     * @returns {AxiosPromise}
     */
    request[method] = (url, params = {}, options = {}, urlType) => {
        return baseRequest(
            Object.assign({url, params, method, urlType}, defaultOpt, options)
        );
    };
});

export default request

